<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        div {
            height: 50px;
            background-color: blueviolet;
            transition: 1s;
            width: 0%;
        }
    </style>
</head>

<body>
    <div></div>
    <input type="file">
    <script>
        let input = document.querySelector('input')
        input.onchange = function () {
            let file = this.files[0]
            let fd = new FormData()
            fd.append('file_data', file)
            console.log(fd)
            let xhr = new XMLHttpRequest()
            xhr.open('post', 'http://127.0.0.1:3001/uploadFile')
            xhr.onload = function () {
                console.log(xhr.responseText);
            }
            xhr.upload.onprogress = function (event) {
                // 文件的总大小 event.total; 已经上传了的文件大小 event.loaded
                let { total, loaded } = event
                let rate = parseInt((loaded / total) * 100) + '%'
                console.log(rate);
                document.querySelector('div').style.width = rate
            }
            xhr.send(fd)
        }
    </script>
</body>

</html>